package com.dabaozhan.modules.wx_app.facade.impl;

import com.dabaozhan.common.utils.Constants;
import com.dabaozhan.common.utils.RandomUtils;
import com.dabaozhan.common.utils.Utils;
import com.dabaozhan.modules.wx_app.base.ResponseDto;
import com.dabaozhan.modules.wx_app.entity.*;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.facade.OrderFacade;
import com.dabaozhan.modules.wx_app.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author houzhong
 * @date 2019-01-25 14:11
 */

@Service("orderFacade")
public class OrderFacadeImpl implements OrderFacade {

    /**用户*/
    @Autowired
    private UserService userService;

    @Autowired
    private PayPassService payPassService;

    @Autowired
    private SiteOrderService siteOrderService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private AccountLogService accountLogService;

    @Override
    public ResponseDto<SiteOrderEntity> addOrderLog(SiteOrderEntity v) {
        ResponseDto<SiteOrderEntity> responseDto = new ResponseDto<SiteOrderEntity>();
        SiteOrderEntity siteOrder = siteOrderService.findById(v.getId());
        if (siteOrder != null) {
            PayPassEntity payPass = payPassService.findByPayUserId(v.getUserId());
            if (!"1".equals(v.getPaymentType())) {
                if (payPass != null) {
                    String password = Utils.md5(v.getPassword(), Constants.PAY_PASS_CACHE_KEY);
                    if (!payPass.getPassword().equals(password)) {
                        responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_501);
                        return responseDto;
                    }
                }
            }

            //状态:1进站 2定价 3待审核 4未支付 5已结算 6 取消
            siteOrder.setOrderStatus("5");
            //支付方式 1 线下 2 微信 3 银行卡 4 余额
            siteOrder.setPaymentType(v.getPaymentType());
            //支付状态 1 支付成功 2 未支付
            siteOrder.setPaymentStatus(1);
            //修改订单状态
            siteOrderService.updateSiteOrder(siteOrder);

            //日志
            AccountLogEntity accountLogVo = new AccountLogEntity();
            //查询供应商是否存在
            UserEntity suppUser = userService.findByUserId(v.getSupplierId());
            if (suppUser != null) {
                //支付方式 1 线下 2 微信 3 银行卡 4 余额
                if (!"1".equals(v.getPaymentType())) {
                    //收入方
                    AccountEntity account = accountService.findByUserId(siteOrder.getSupplierId());
                    //总价
                    account.setAmount(account.getAmount() + v.getMoneyTotal().floatValue());
                    //修改收入总价
                    accountService.updateSiteAccount(account);

                    //账户余额
                    accountLogVo.setAmount(account.getAmount());
                    //账户id
                    accountLogVo.setAccountId(account.getId());
                    //订单编号
                    accountLogVo.setBusinessNumber(v.getOrderNumber());
                    //编号
                    accountLogVo.setAccountNumber(RandomUtils.random6());
                    //回收员类型 1 充值 2 提现 3 支出 4 收入
                    accountLogVo.setBusinessType("4");
                    //总价格
                    accountLogVo.setPrice(v.getMoneyTotal().floatValue());
                    //接收人
                    accountLogVo.setUserId(v.getSupplierId());
                    //支付方式 1 线下 2 微信 3 银行卡 4 余额
                    accountLogVo.setPaymentType(4);
                    //添加明细
                    accountLogService.saveSiteAccountLog(accountLogVo);
                    /*** ------------------账户收入方 结束------------------- */
                }
            }
            //支付方式 1 线下 2 微信 3 银行卡 4 余额
            if (!"1".equals(v.getPaymentType())) {
                //支出方
                AccountEntity account2 = accountService.findByUserId(siteOrder.getUserId());
                //总价
                account2.setAmount(account2.getAmount() - v.getMoneyTotal().floatValue());
                //修改收入总价
                accountService.updateSiteAccount(account2);

                //日志
                accountLogVo = new AccountLogEntity();
                //账户余额
                accountLogVo.setAmount(account2.getAmount());
                //账户id
                accountLogVo.setAccountId(account2.getId());
                //订单编号
                accountLogVo.setBusinessNumber(v.getOrderNumber());
                //编号
                accountLogVo.setAccountNumber(RandomUtils.random6());
                //回收员类型 1 充值 2 提现 3 支出 4 收入
                accountLogVo.setBusinessType("3");
                //总价格
                accountLogVo.setPrice(v.getMoneyTotal().floatValue());
                //支出方
                accountLogVo.setUserId(v.getUserId());
                //支付方式 1 线下 2 微信 3 银行卡 4 余额
                accountLogVo.setPaymentType(4);
                //添加明细
                accountLogService.saveSiteAccountLog(accountLogVo);

                /*** ------------------账户支出方 结束 ------------------- */
            }
        }

        responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
        return responseDto;
    }
}
